// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections;
using System.Text;


namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Summary description for QuoteDetail.
	/// </summary>
	public class QuoteDetail
	{
      private Quote     m_objQuote = null;  // Parent quote that owns this item
      private string    m_strConfigRuleFileName = "";  // Not tracked by database.
      private string    m_strCategoryId;
      private Guid      m_guidConfigParentId;
      private Decimal   m_nExtendedAmount;
      private int       m_nLineItemNumber;
      private string    m_strName;
      private Guid      m_guidQuoteId;
      private Guid      m_guidQuoteDetailId;
      private Decimal   m_nQuantity;
      private Guid      m_guidProductId;
      private Decimal   m_nPricePerUnit;
      private string    m_strProductDescription;
      private string    m_strProductNumber;
      private string    m_strShipTo_Fax;
      private string    m_strShipTo_Line3;
      private Guid      m_guidUnitOfMeasureId;

		public QuoteDetail()
		{
		   	
		}

    
      public string CategoryId
      {
         get 
         { 
            return m_strCategoryId;
         }
         set 
         { 
            m_strCategoryId = value;
         }

      }
            
      public Guid ConfigParentId
      {
         get 
         { 
            return m_guidConfigParentId;
         }
         set 
         { 
            m_guidConfigParentId = value;
         }
      }

      public string ConfigRuleFileName
      {
         get { return m_strConfigRuleFileName;}
         set { m_strConfigRuleFileName = value;}
      }
      
      public Decimal ExtendedAmount
      {
         get { return m_nExtendedAmount;}
         set { m_nExtendedAmount = value;}
      }

      public int LineItemNumber
      {
         get { return m_nLineItemNumber;}
         set { m_nLineItemNumber = value;}
      }

      public string Name
      {
         get { return m_strName;}
         set { m_strName = value;}
      }

      public Quote QuoteOwner
      {
         get { return m_objQuote;}
         set { m_objQuote = value;}
      }

      public Guid QuoteId
      {
         get { return m_guidQuoteId;}
         set { m_guidQuoteId = value;}
      }
      
      public Guid QuoteDetailId
      {
         get { return m_guidQuoteDetailId;}
         set { m_guidQuoteDetailId = value;}
      }

      public Decimal Quantity
      {
         get { return m_nQuantity;}
         set { m_nQuantity = value;}
      }

      public Decimal PricePerUnit
      {
         get { return m_nPricePerUnit;}
         set { m_nPricePerUnit = value;}
      }
      
      public string ProductDescription
      {
         get { return m_strProductDescription;}
         set { m_strProductDescription = value;}
      }

      public Guid ProductId
      {
         get { return m_guidProductId;}
         set { m_guidProductId = value;}
      }
      
      public string ProductNumber
      {
         get { return m_strProductNumber;}
         set { m_strProductNumber = value;}
      }
      
      public string ShipToFax
      {
         get { return m_strShipTo_Fax;}
         set { m_strShipTo_Fax = value;}
      }
      
      public string ShipToLine3
      {
         get { return m_strShipTo_Line3;}
         set { m_strShipTo_Line3 = value;}
      }

      public Guid UnitOfMeasureId
      {
         get { return m_guidUnitOfMeasureId;}
         set { m_guidUnitOfMeasureId = value;}
      }

      
      public decimal CalculatePrice()
      {
         decimal     nPrice = this.PricePerUnit;   
         ArrayList   objChildItems = this.GetChildItemList();

         foreach (QuoteDetail objItem in objChildItems)
         {
            nPrice += objItem.PricePerUnit;   
         }

         return nPrice;
      }
  
      // Return all of the child items of this QuoteDetail
      public ArrayList GetChildItemList()
      {
         ArrayList   objResultList = new ArrayList(10);

         foreach (QuoteDetail item in this.QuoteOwner.LineItems)
         {
            if (item.ConfigParentId == this.QuoteDetailId)
            {
               objResultList.Add(item);
            }
         }        

         return objResultList;
      }

      // Search a line item's children to find a matching ProductNumber
      public QuoteDetail FindChildItem(string strProductNumber)
      {
         QuoteDetail    objMatchingItem = null;
         
         foreach (QuoteDetail item in this.QuoteOwner.LineItems)
         {
            if (item.ConfigParentId == this.QuoteDetailId)
            {
               if (item.ProductNumber == strProductNumber)
               {
                  objMatchingItem = item;
                  break;
               }
            }
         }        
         
         return objMatchingItem;
      }

      // Search a line item's children to find a matching ConfigId
      public QuoteDetail FindChildItem(string strProductNumber, string strCategoryId)
      {
         QuoteDetail    objMatchingItem = null;
         
         foreach (QuoteDetail item in this.QuoteOwner.LineItems)
         {
            if (item.ConfigParentId == this.QuoteDetailId)
            {
               if (item.ProductNumber == strProductNumber && item.CategoryId == strCategoryId)
               {
                  objMatchingItem = item;
                  break;
               }
            }
         }        
         
         return objMatchingItem;
      }

      public void Init(Quote objQuote, QuoteDetail objParentItem, Product objProduct)
      {
         Init(objQuote, objProduct);
         this.ConfigParentId  = objParentItem.QuoteDetailId;
      }
      
      public void Init(Quote objQuote, Product objProduct)
      {
         m_objQuote = objQuote;
         this.UnitOfMeasureId = objProduct.DefaultUnitOfMeasureId;
         this.ConfigParentId = Guid.Empty;
         this.m_nLineItemNumber = 1;	
         this.m_strName = objProduct.Name;	
         this.m_guidQuoteId = m_objQuote.QuoteId;	
         this.m_nQuantity = 1;	
         this.m_guidProductId = objProduct.ProductId;	
         this.ProductNumber = objProduct.ProductNumber;
         this.m_nPricePerUnit = objProduct.Price;	         
      }
      
      // Return this item as an XML string
      /*
         <?xml version=\"1.0\" encoding=\"utf-16\"?>
         <quotedetail>
            <description>Product added by SDK</description>
            <priceperunit>159</priceperunit>
            <productid>{81AF6FD8-E289-4125-9EE7-6B00CE638492}</productid>
            <quantity>1</quantity>
            <quoteid>{EBD886AC-DBC0-481B-832D-936EBFDC42E6}</quoteid>
            etc etc
         </quotedetail>
      */
      public string ToXmlString()
      {
         StringBuilder sb = new StringBuilder(700);
   
         sb.Append("<?xml version=\"1.0\" encoding=\"utf-16\"?>");
         sb.Append("<quotedetail>\r\n");    
         sb.Append("   <qadv_config_parent_id> {" + m_guidConfigParentId.ToString() + "} </qadv_config_parent_id>\r\n");
         sb.Append("   <qadv_config_category_id>" + m_strCategoryId + "</qadv_config_category_id>\r\n");
         sb.Append("   <description>" + m_strName + "</description>\r\n");
         sb.Append("   <priceperunit>" + m_nPricePerUnit.ToString() + "</priceperunit>\r\n");
         sb.Append("   <productid>{" + m_guidProductId.ToString() + "}</productid>\r\n");
         sb.Append("   <quantity>" + m_nQuantity.ToString() + "</quantity>\r\n");
         sb.Append("   <quoteid>{" + m_guidQuoteId.ToString() + "}</quoteid>\r\n");
         sb.Append("   <shipto_line3>" + m_strShipTo_Line3 + "</shipto_line3>\r\n");
         sb.Append("   <shipto_fax>" + m_strShipTo_Fax + "</shipto_fax>\r\n");
         sb.Append("   <uomid>{" + m_guidUnitOfMeasureId.ToString() + "}</uomid>\r\n");              
         sb.Append("</quotedetail>\r\n");

         return sb.ToString();
      }

	} // end class
}
